# -*- coding:utf-8 -*-

from pynput.mouse import Button
from common.data import DB
import time
import uuid


def move_mouse(x, y):
    if DB.isDrag:
        pass
    pass


"""

点击：单击，双击，（不搞三击了，用不到）

拖动：
1、按下后，回到原坐标再松开 （不想搞这个，这个东西，又拖回来，那还是拖动么）
2、按下左键，拖动到指定位置，再按右键，再松开左键
"""


def click_mouse(x, y, button: Button, pressed):
    # print(f"{x},{y},{button},{pressed}")

    # 按下时：
    if pressed:
        # 当前按下的时间
        pressTime = time.time()
        DB.pressTime = pressTime
        try:
            sleepTime = pressTime - DB.pressTime
            DB.pressTime = pressTime
        except Exception:
            sleepTime = 0.5
        # 上一次按下的时间
        try:
            beforeButtonPressTime = DB.beforePressed[button.name]["pressTime"]
            # 两次鼠标按钮，点击的时间差
            time_difference = pressTime - beforeButtonPressTime
        except KeyError:
            # 第一次启动时，没有该字段时，默认时间差0.5秒
            time_difference = 0.5

        if time_difference < 0.3 and DB.beforePressed[button.name]["isDoubleClick"] == False:  # 小于300毫秒，判断为双击

            DB.beforePressed[button.name] = {"position": {"x": x, "y": y}, "pressTime": pressTime,
                                             "isDoubleClick": True, "sleepTime": sleepTime}
            # 双击
            click_action = {"id": str(uuid.uuid4()),
                            "type": "mouse", "action": "doubleClick", "button": button.name,
                            "sleepTime": sleepTime,
                            "position": {"x": x, "y": y}, "duration": time_difference
                            }

            # 把前一次点击的事件pop掉
            DB.caseData["cases"].pop()
            DB.caseData["cases"].append(click_action)
        else:
            DB.beforePressed[button.name] = {"position": {"x": x, "y": y}, "pressTime": pressTime,
                                             "isDoubleClick": False, "sleepTime": sleepTime}

    else:
        # 按键UP
        # action: click ,doubleClick,drag
        pressedTime = time.time()
        position = DB.beforePressed[button.name]["position"]
        pressTime = DB.beforePressed[button.name]["pressTime"]
        sleepTime = DB.beforePressed[button.name]["sleepTime"]
        duration = pressedTime - pressTime
        if {"x": x, "y": y} != position:
            # 拖动
            click_action = {"id": str(uuid.uuid4()),
                            "type": "mouse", "action": "drag", "button": button.name, "sleepTime": sleepTime,
                            "position": {"x": position["x"], "y": position["y"], "tox": x, "toy": y},
                            "duration": duration
                            }
            DB.caseData["cases"].append(click_action)
        elif DB.beforePressed[button.name]["isDoubleClick"]:
            pass
        else:
            # 单击
            click_action = {"id": str(uuid.uuid4()),
                            "type": "mouse", "action": "click", "button": button.name, "sleepTime": sleepTime,
                            "position": {"x": x, "y": y}, "duration": duration
                            }
            DB.caseData["cases"].append(click_action)

    # try:
    #     if not pressed:
    #         print(DB.caseData["cases"])
    # except IndexError:
    #     pass


def scroll_middle(x, y, dx, dy):
    pressTime = time.time()
    DB.pressTime = pressTime
    print('Scrolled {0} at {1}'.format('down' if dy < 0 else 'up', (x, y)))

    click_action = {"id": str(uuid.uuid4()),
                    "type": "mouse", "action": "scroll", "button": "middle", "position": {"x": x, "y": y},
                    "scroll": {"dx": dx, "dy": dy}, "duration": 4
                    }
    DB.caseData["cases"].append(click_action)
    # print(DB.caseData)
